package com.seaboxdata.threadHandler;

import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.BlockMonitoring;
import com.seaboxdata.kafka.BaseStringKafkaConsumer;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import com.seaboxdata.service.BlockMonitoringService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @create 2020-11-09 9:20
 **/
@Component
@Slf4j
public class BlockMonitoringHandler implements DisposableBean, Runnable {

    private Thread thread;

    @Autowired
    private BlockMonitoringService blockMonitoringService;

    @Autowired
    private BaseStringKafkaConsumer baseStringKafkaConsumer;

    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;

    @Value("${real.trans.blockMonitoringTopic}")
    private String topic;

    @Value("${interface.isTest}")
    private String isTest;

    private boolean flag;

    @Autowired
    public void initThread() {
        if("true".equals(isTest)){
            return;
        }
        this.flag = true;
        this.thread = new Thread(this);
        thread.setName("blockMonitoringHandlerThread");
        this.thread.start();
        log.info("blockMonitoringHandlerThread start");
    }

    @Override
    public void run() {
        boolean success = true;
        KafkaConsumer<String, String> consumer = baseStringKafkaConsumer.getConsumer(topic);
        while (flag) {
            ConsumerRecords<String, String> messages = consumer.poll(Duration.ofMillis(100L));
            List<BlockMonitoring> results = new ArrayList<>();
            for (ConsumerRecord<String, String> message : messages) {
                results.add(JSONObject.parseObject(message.value(), BlockMonitoring.class));
            }
            //写入数据库
            if (!CollectionUtils.isEmpty(results)) {
                try {
                    blockMonitoringService.saveBatch(results);
                    consumer.commitSync();
                } catch (Exception e) {
                    log.error("whp blockMonitoring commit offset failed");
                    //success = false;
                }

                /*if (success) {
                    try {
                        blockMonitoringService.saveBatch(results);
                    } catch (Exception e) {
                        log.error("batch insert blockMonitoring failed");
                        e.printStackTrace();
                        for (BlockMonitoring blockMonitoring : results) {
                            try {
                                baseStringKafkaProducer.sendMessageToKafka(JSONObject.toJSONString(blockMonitoring), topic);
                            } catch (Exception e1) {
                                log.error("write database failed and write kafka blockMonitoring error");
                                e1.printStackTrace();
                            }
                        }
                    }
                }*/
            }
        }
    }

    @Override
    public void destroy() throws Exception {
        this.flag = false;
    }
}
